namespace Digitalmes.Common.Result;

/// <summary>
/// <see cref="ApiResult"/> 包装器。
/// </summary>
/// <remarks>
/// 数据格式为：
/// {
///     "code": 200,
///     "message": "",
///     "data": {}
/// }
/// </remarks>
public static class JResult
{
    /// <summary>
    /// 返回服务端指定错误
    /// </summary>
    /// <param name="code">错误代码</param>
    /// <param name="errMsg">错误消息</param>
    /// <returns></returns>
    public static ApiResult Error(int code, string errMsg)
    {
        return new ApiResult
        {
            Code = code,
            Message = errMsg
        };
    }

    /// <summary>
    /// 返回服务端指定错误，状态码为 -1
    /// </summary>
    /// <param name="errMsg">错误描述</param>
    /// <returns></returns>
    public static ApiResult Error(string errMsg)
    {
        return new ApiResult
        {
            Code = -1,
            Message = errMsg
        };
    }

    /// <summary>
    /// 返回服务端错误，状态码为 500
    /// </summary>
    /// <returns></returns>
    public static ApiResult Error()
    {
        return new ApiResult
        {
            Code = (int)HttpStatusCode.InternalServerError,
            Message = "服务端发生错误"
        };
    }

    /// <summary>
    /// 返回成功结果，状态码为 200
    /// </summary>
    /// <returns></returns>
    public static ApiResult Success()
    {
        return new ApiResult
        {
            Code = (int)HttpStatusCode.OK
        };
    }

    /// <summary>
    /// 返回成功结果，状态码为 200
    /// </summary>
    /// <param name="data">返回数据</param>
    /// <returns></returns>
    public static ApiResult<TObject> Success<TObject>(TObject data)
    {
        return new ApiResult<TObject>
        {
            Code = (int)HttpStatusCode.OK,
            Data = data
        };
    }
}
